package com.mj.alexarank.service;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.mj.alexarank.util.HttpUtils;
import com.mj.alexarank.util.RegUtil;

/**
 * Alexa网站排名
 * @author zhaominglei
 * @date 2015-3-24
 * 
 */
public class AlexarankService extends BaseService {
	@SuppressWarnings("unused")
	private static final String TAG = AlexarankService.class.getSimpleName();
	private static final String ALEXA_URL = "http://www.alexa.cn/index.php"; //Alexa
	private static final String ALEXA_API_URL = "http://www.alexa.cn/api0523.php"; //Alexa API
	private static final String ALEXA_SERVERINFO_URL = "http://www.alexa.cn/inc.php"; //Alexa
	private static final String separator = ": ";
	private static final String[] rankTitles = new String[]{"当日排名", "排名变化趋势", "一周平均排名", "排名变化趋势", "一月平均排名", "排名变化趋势", "三月平均排名", "排名变化趋势"};
	private static final String[] serverInfoTitles = new String[]{"Google PR", "服务器IP", "IP所在地", "服务器类型", "协议类型", "页面类型"};
	
	public String getAlexarank(String domainname) {
		domainname = domainname.replaceAll("http://", "").replaceAll("www.", "").replaceAll("www", "");
		if (domainname == null || domainname.equals("")) {
			return null;
		}
		StringBuilder queryURL = new StringBuilder(ALEXA_URL);
		queryURL.append("?url=").append(domainname);
		String html = HttpUtils.doGetForAlexa(domainname, queryURL.toString(), null);
		if (html != null && !html.equals("")) {
			String params = RegUtil.getMatchRegStr(html, "showHint\\(\\'(.+?)\\'\\)");
			if (params == null || params.equals("")) {
				return null;
			}
			String[] paramArray = params.split(",");
			if (paramArray.length != 3) {
				return null;
			}
			String sig = paramArray[1];
			String keyt = paramArray[2];
			StringBuilder result = new StringBuilder();
			String beian = RegUtil.getMatchRegStr(html, "<div class=\"biaoge\" style=\"height:75px; overflow:hidden\">(.+?)</div>");
			beian = beian.replaceAll("<font>", "").replaceAll("</font>", "").replaceAll("<br />", separator);
			Document document = Jsoup.parse(beian);
			Elements elements = document.getElementsByTag("li");
			for (Element element : elements) {
				String content = element.text();
				if (content.indexOf("其他") > -1) continue;
				result.append(content).append("\n");
			}
			result.append("\n");
			StringBuilder param = new StringBuilder();
			param.append("url=").append(domainname).append("&sig=").append(sig);
			param.append("&keyt=").append(keyt);
			String paiming = HttpUtils.doPostForAlexa(domainname, ALEXA_API_URL, param.toString(), null);
			String[] pmArray = paiming.split("\\*");
			for (int i = 0; (pmArray != null && pmArray.length > 0) && i < pmArray.length; i++) {
				String content = pmArray[i];
				if (i <= 7) {
					content = content.replaceAll("\\-", "升").replaceAll("\\+", "降");
					result.append(rankTitles[i]).append(separator).append(content).append("\n");
				}
			}
			result.append("\n");
			String serverInfo = HttpUtils.doPostForAlexa(domainname, ALEXA_SERVERINFO_URL, "url="+domainname, null);
			String[] siArray = serverInfo.split("\\|\\|");
			for (int i = 0; (siArray != null && siArray.length > 0) && i < siArray.length; i++) {
				String content = siArray[i];
				result.append(serverInfoTitles[i]).append(separator).append(content).append("\n");
			}
			return result.toString();
		}
		return null;
	}
}
